import {
	defineStore
} from 'pinia';
import {
	ref
} from 'vue'
import http from '/utils/request.js'

export const useUserStore = defineStore('user', () => {
	const userInfo = ref({})
	const token = ref('')
	const refreshToken = ref('')
	const isLogin = ref(false)
	const rememberMe = ref(false)

	const login = async (loginname, password, remember) => {
		try {
			const res = await http.post('/users/login', {
				loginname,
				password
			})

			if (res.code === '200') {
				token.value = res.data.token
				refreshToken.value = res.data.refreshToken
				userInfo.value = {
					userid: res.data.userid
				}
				isLogin.value = true
				rememberMe.value = remember

				getUserInfo()

				return true
			}
			return false
		} catch (error) {
			console.error('登录失败:', error)
			throw error
		}
	}

	const register = async (username, password) => {
		try {
			const res = await http.post('/users/register', {
				username,
				password,
				name,
				email,
				role,
				classId
			})
			return res.code === '200'
		} catch (error) {
			console.error('注册失败:', error)
			throw error
		}
	}

	const getUserInfo = async () => {
		try {
			const userid = userInfo.value.userid
			if (!userid) return
			const res = await http.get('/users/me', {
				userid
			})
			userInfo.value = res.data[0]
			if (!userInfo.value.avatar) {
				userInfo.value.avatar = 'https://free.picui.cn/free/2025/08/20/68a5170b9bf75.jpg'
			}

			return res.code === '200'
		} catch (e) {
			console.error(e)
		}
	}

	const logout = () => {
		userInfo.value = {}
		token.value = ''
		refreshToken.value = ''
		isLogin.value = false

		uni.reLaunch({
			url: '/pages/auth/auth'
		})
	}

	const changePwd = async (oldpassword, newpassword) => {
		try {
			const userid = userInfo.value.userid
			if (!userid) return
			const res = await http.get('/users/change-password', {
				userid,
				loginname,
				password
			})
			return res.code === '200'
		} catch (error) {
			console.error('修改失败:', error)
			throw error
		}
	}

	/**
	 * 绑定头像
	 * @param {String} avatar 头像地址  
	 */
	const bindAvatar = async (avatar) => {
		try {
			const userid = userInfo.value.userid
			if (!userid) return
			const res = await http.get('/user/binduseravatar', {
				userid,
				avatar
			})
			return res.code === '200'
		} catch (error) {
			console.error('绑定失败:', error)
			throw error
		}
	}

	const bindUserName = async (username) => {
		try {
			const userid = userInfo.value.userid
			if (!userid) return
			const res = await http.get('/user/bindusername', {
				userid,
				username
			})
			return res.code === '200'
		} catch (error) {
			console.error('绑定失败:', error)
			throw error
		}
	}

	/**
	 * 上传图片
	 * @param {FormData} img 图片数据  
	 */
	const uploadImg = async (img) => {
		try {
			const res = await http.upload('/file/upload', {
				img
			})
			if (res.fileUrl) {
				return 'http://47.94.6.186:3001' + res.fileUrl
			}
			return false
		} catch (error) {
			console.error('上传失败:', error)
			throw error
		}
	}

	return {
		userInfo,
		token,
		refreshToken,
		isLogin,
		rememberMe,
		login,
		register,
		getUserInfo,
		logout,
		changePwd,
		bindAvatar,
		bindUserName,
		uploadImg
	}
}, {
	persist: {
		getItem: uni.getStorageSync,
		setItem: uni.setStorageSync,
		pick: ['userInfo', 'token', 'refreshToken', 'rememberMe']
	},
});